package com.framework.common.util.pinyin;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 龘鵺
 * @className com.framework.common.util.pinyin
 * @description 拼音工具类
 * @datetime 2025-01-09 11:11:11
 * @version 1.0
 */
public class PinYinUtil {

    /**
     * @title 将字符串中的中文转化为拼音, 其他字符不变
     * @description 获取的是三个值， 中文转拼音的首字的首字母大写， 中文转拼音的首字母大写其他字母小写， 全部字的首字母大写
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     * @param chinese 1
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    public static Map<String, String> toPinYinMap(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] input = chinese.trim().toCharArray();
        //全拼音
        StringBuilder pinYinStr = new StringBuilder();
        //第一个字的首字母
        StringBuilder initialStr = new StringBuilder();
        //拼音首字母
        StringBuilder pinYinAcronymStr = new StringBuilder();
        Map<String, String> map = new HashMap<>(3);
        try {
            for (int i = 0; i < input.length; i++) {
                String value = "";
                if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    if (temp != null && temp.length > 0) {
                        value = temp[0];
                        String initialStrValue = value.substring(0, 1).toUpperCase();
                        pinYinAcronymStr.append(initialStrValue);
                        pinYinStr.append(initialStrValue + value.substring(1));
                        if (initialStr.length() == 0) {
                            initialStr.append(initialStrValue);
                        }
                    }
                } else {
                    value = Character.toString(input[i]);
                    pinYinStr.append(value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("pinYinStr", pinYinStr.toString());
        map.put("initialStr", initialStr.toString());
        map.put("pinYinAcronymStr", pinYinAcronymStr.toString());
        return map;
    }


    /**
     * @param chinese 1 汉字串
     * @return java.lang.String
     * @title 将字符串中的中文转化为拼音, 其他字符不变 - 全小写
     * @description 将字符串中的中文转化为拼音, 其他字符不变 - 全小写
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    public static String toPinYin(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] input = chinese.trim().toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        try {
            for (int i = 0; i < input.length; i++) {
                if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    stringBuilder.append(temp[0].substring(0, 1).toUpperCase() + temp[0].substring(1));
                } else {
                    stringBuilder.append(Character.toString(input[i]));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * @param chinese 1 汉字串
     * @return java.lang.String
     * @title 获取汉字串拼音首字母，英文字符不变
     * @description 获取汉字串拼音首字母，英文字符不变
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    public static String getFirstSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (temp != null) {
                        pybf.append(temp[0].charAt(0));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().replaceAll("\\W", "").trim();
    }

}
